package com.itheima.vms.controller;

import com.itheima.vms.domain.PageResult;
import com.itheima.vms.domain.dto.ItemDateDto;
import com.itheima.vms.domain.dto.ItemUpdateDto;
import com.itheima.vms.domain.vo.ItemEchoVo;
import com.itheima.vms.domain.vo.ItemListVo;
import com.itheima.vms.response.Result;
import com.itheima.vms.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    //分页
    @GetMapping
    public Result page(Integer page,Integer pageSize,String code,String title,String itemCategory){
        PageResult<ItemListVo> pageResult= itemService.page(page,pageSize,code,title,itemCategory);
        return Result.ok(pageResult);
    }

    @GetMapping("/list")
    public Result<List<Map<String, Object>>> getItemList() {
        try {
            List<Map<String, Object>> itemList = itemService.getItemList();
            return Result.ok(itemList);
        } catch (Exception e) {
            return Result.error("获取商品列表失败: " + e.getMessage());
        }
    }


    //添加
    @PostMapping
    public Result addItem(@RequestBody ItemDateDto dto){
         return Result.ok(itemService.addItem(dto));
    }

    @GetMapping("/{id}")
    public Result getItemDateById(@PathVariable Integer id){
        ItemEchoVo vo = itemService.getItemDateById(id);
        return Result.ok(vo);
    }

    //修改
    @PutMapping
    public Result updateItem(@RequestBody ItemUpdateDto dto){
        return Result.ok(itemService.updateItemDate(dto));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result deleteItem(@PathVariable Integer id){
        return Result.ok(id);
    }
}
